package com.goktech.olala.client.controller.order;

import com.goktech.olala.core.resp.RespBuyInfoVo;
import com.goktech.olala.core.service.ICustomerConsigneeService;
import com.goktech.olala.core.service.IOrderDetailService;
import com.goktech.olala.core.service.IOrderMasterService;
import com.goktech.olala.core.service.IShopCartService;
import com.goktech.olala.server.pojo.customer.Customer;
import com.goktech.olala.server.pojo.customer.CustomerConsignee;
import com.goktech.olala.server.pojo.order.OrderMaster;
import com.goktech.olala.server.pojo.order.ShopCart;
import com.goktech.olala.utils.RMBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname OrderDetailController
 * @Description TODO
 * @Date 2020/12/24 9:05
 * @Created by ASUS
 */
@Controller
@RequestMapping("/orderApi")
public class OrderDetailController {

    ModelAndView mav = new ModelAndView();

    @Autowired
    ICustomerConsigneeService customerConsigneeService;
    @Autowired
    IShopCartService shopCartService;
    @Autowired
    IOrderDetailService orderDetailService;
    @Autowired
    IOrderMasterService orderMasterService;

    //直接从商品页面购买
    @RequestMapping("/settleOrder.do")
    public ModelAndView settleOrder(HttpServletRequest request){
        HttpSession session = request.getSession();
        //通过id找到地址集合
        String customerId = (String) session.getAttribute("customerId");

        List<CustomerConsignee> customerConsigneeList = customerConsigneeService.selectByCustomerId(customerId);

        mav.addObject("consigneeVoList",customerConsigneeList);

        /*source=0
        goodsId=1596677676
        goodsName=xxx
        shopPrice=3400
        goodsAmount=1
        realBuyPrice=3145
        marketPrice=3600*/
        //把杂七杂八的东西拿到
        String goodsId = request.getParameter("goodsId");
        String goodsname = request.getParameter("goodsName");
        String price = request.getParameter("shopPrice");
        String amount = request.getParameter("goodsAmount");
        String buyPrice = request.getParameter("realBuyPrice");
        String market = request.getParameter("marketPrice");

        String goodsName = null;
        try {
            goodsName = URLDecoder.decode(goodsname, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Integer goodsAmount = Integer.parseInt(amount);
//        Integer realBuyPrice = Integer.valueOf(RMBUtil.changeY2F(buyPrice));
        Integer realBuyPrice = Integer.parseInt(buyPrice);
        Integer marketPrice = Integer.valueOf(RMBUtil.changeY2F(market));
        //订单页面的商品集合
        List<RespBuyInfoVo> buyGoodsList = new ArrayList<>();
        //把商品信息传到订单商品类
        RespBuyInfoVo respBuyInfoVo = new RespBuyInfoVo();
        respBuyInfoVo.setCustomerId(customerId);
        respBuyInfoVo.setGoodsId(goodsId);
        respBuyInfoVo.setGoodsName(goodsName);
        respBuyInfoVo.setGoodsAmount(goodsAmount);
        respBuyInfoVo.setGoodsPrice(realBuyPrice);
        respBuyInfoVo.setOrderMoney(realBuyPrice*goodsAmount);
        //商品流水号
        String goodSn = String.valueOf(System.currentTimeMillis());
        respBuyInfoVo.setGoodSn(goodSn);

        //把商品类加到订单集合内
        buyGoodsList.add(respBuyInfoVo);

        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setCustomerId(customerId);
        //订单流水号===商品流水号
        orderMaster.setOrderSn(goodSn);
        orderMaster.setOrderMoney(realBuyPrice*goodsAmount);
         //把订单添加到数据库
//        orderMasterService.insert(orderMaster);
//        //把订单明细添加到数据库
//        int i = orderDetailService.addOrder(respBuyInfoVo);

        mav.addObject("buyGoodsList",buyGoodsList);
        mav.setViewName("home/pay");
        return mav;
    }

    //从购物车里面购买
    @RequestMapping("/settleShopCart.do")
    public ModelAndView settleShopCart(HttpServletRequest request){

        //用户地址
        HttpSession session = request.getSession();
        String customerId = (String) session.getAttribute("customerId");
        List<CustomerConsignee> customerConsigneeList = customerConsigneeService.selectByCustomerId(customerId);
        mav.addObject("consigneeVoList",customerConsigneeList);

        /*一件
        shopCartIdArr=31
        amountArr=1,
        多件
        shopCartIdArr=31,32,
        amountArr=1,2,
        */

        //获得id或id组
        String shopCartIdArr = request.getParameter("shopCartIdArr");
        //将id分隔成数组
        String [] shopCartId = shopCartIdArr.split(",");
        //订单商品集
        List<RespBuyInfoVo> buyGoodsList = new ArrayList<>();
        //订单流水号
        String goodSn = String.valueOf(System.currentTimeMillis());
        //循环赋值
        for (int i = 0; i<shopCartId.length;i++){
            //根据购物车id查询商品然后获得商品/用户等信息
            Integer cartId = Integer.parseInt(shopCartId[i]);
            ShopCart shopCart = shopCartService.selectByPrimaryKey(cartId);
            //新增一个订单显示的商品类,并把商品信息放进去
            RespBuyInfoVo respBuyInfoVo = new RespBuyInfoVo();
            respBuyInfoVo.setCustomerId(shopCart.getCustomerId());
            respBuyInfoVo.setGoodsId(shopCart.getGoodsId());
            respBuyInfoVo.setGoodsName(shopCart.getGoodsName());
            respBuyInfoVo.setGoodsPrice(shopCart.getRealBuyPrice());
            respBuyInfoVo.setGoodsAmount(shopCart.getGoodsAmount());
            respBuyInfoVo.setOrderMoney(shopCart.getRealBuyPrice()*shopCart.getGoodsAmount());
            //流水号
            respBuyInfoVo.setGoodSn(goodSn);
            //把类放进去
            buyGoodsList.add(i,respBuyInfoVo);

            OrderMaster orderMaster = new OrderMaster();
            orderMaster.setCustomerId(customerId);
            //一个订单里面的流水号相同
            orderMaster.setOrderSn(goodSn);
            orderMaster.setOrderMoney(shopCart.getRealBuyPrice()*shopCart.getGoodsAmount());
//            //把订单添加到数据库
//            orderMasterService.insert(orderMaster);
//            //把详细表添加到数据库
//            orderDetailService.addOrder(respBuyInfoVo);


            //把商品加入订单之后，把购物车的东西删除
            shopCartService.deleteByPrimaryKey(cartId);

        }




        mav.addObject("buyGoodsList",buyGoodsList);
        mav.setViewName("home/pay");

        return mav;
    }



    //支付成功页面
    @RequestMapping("/successOrder.do")
    public ModelAndView successOrder(HttpServletRequest request){
        //用户地址
        HttpSession session = request.getSession();
        String customerId = (String) session.getAttribute("customerId");
        List<CustomerConsignee> customerConsigneeList = customerConsigneeService.selectByCustomerId(customerId);

        //总金额:暂时是直接url传值；
        // 之后是否可以通过上面的订单查看方法，通过id得到总金额，会比较安全
        String orderMoney = request.getParameter("orderMoney");
        //得到流水号
        String goodSn = request.getParameter("goodSn");

        mav.addObject("orderMoney",orderMoney);
        mav.addObject("goodSn",goodSn);
        mav.addObject("consigneeVoList",customerConsigneeList);

        mav.setViewName("home/success");
        return mav;
    }


}
